﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class UserDAO:DataProvider
    {
        public List<UserDTO> GetAll()
        {
            SqlConnection connection = MoKetNoi();
            string sql = "SELECT * FROM  [User]";
            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader();
            List<UserDTO> dskh = new List<UserDTO>();
            UserDTO pb;
            while (reader.Read())
            {
                pb = new UserDTO();
                pb.Mauser = reader.GetString(0);
                pb.Manv = reader.GetString(1);
                pb.Username = reader.GetString(2);
                pb.Pass = reader.GetString(3);
                dskh.Add(pb);
            }
            DongKetNoi(connection);
            return dskh;
        }
        public bool Insert(UserDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "insert into [User](Ma_User,Ma_NV,UserName,Pass) values('" + lp.Mauser + "','" + lp.Manv + "','" + lp.Username + "','" + lp.Pass + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
            DongKetNoi(connection);
            return kq;
        }
        public bool Update(UserDTO p)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "update [User] set Ma_NV = ('" + p.Manv + "'),UserName = ('" + p.Username + "'),Pass = ('" + p.Pass + "') where Ma_User = ('" + p.Mauser + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
            DongKetNoi(connection);
            return kq;
        }
        public bool Delete(UserDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "delete from [User] where Ma_User = ('" + lp.Mauser + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
            DongKetNoi(connection);
            return kq;
        }
        public bool selectlike(UserDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "SELECT * FROM  [User] WHERE UserName = ('" + lp.Username + "') and Pass = ('" + lp.Pass + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            DataTable tb = new DataTable();
            tb.Load(command.ExecuteReader());
            if (tb.Rows.Count == 0)
            {
                kq = false;
            }
            return kq;

        }
    }
}
